package nix.demo2.dao;

import java.io.InputStream;
import java.util.Properties;

public class DaoFactory {

	private static Properties props = null;

	static {
		InputStream in = DaoFactory.class.getClassLoader().getResourceAsStream(
				"dao.properties");
		props = new Properties();
		try {
			props.load(in);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static UserDao getUserDao() {

		// 通过配置文件获得实现类名称
		String daoClassName = props.getProperty("nix.demo2.dao.UserDao");

		// 通过反射创建实现类对象
		try {
			Class clazz = Class.forName(daoClassName);
			return (UserDao) clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
